package org.kealinghornets.jchuah.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Toast;


public class MyActivity2 extends Activity {
    int numCleared = 0;
    public void onclockbuttonpush00(final View view){
        Log.d("kealinghornets.myApp", "the button was pushed");
        Animation fade_out_animation=AnimationUtils.loadAnimation(this, R.anim.fade_out);
        fade_out_animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                view.setEnabled(false);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.setEnabled(true);
                view.setAlpha(0);
                view.clearAnimation();
                Log.d("animation", "view alpha: " + view.getAlpha());


            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        Animation fade_in_animation=AnimationUtils.loadAnimation(this, R.anim.fade_in);
        fade_in_animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                view.setAlpha(1);
                view.setEnabled(false);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.setEnabled(true);
                view.setAlpha(1);
                view.clearAnimation();

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });



        if (view.getAlpha()==1){
            Log.d("animation", "fade out");
            view.clearAnimation();
            view.startAnimation(fade_out_animation);
            numCleared = numCleared + 1;
        }else{
            Log.d("animation", "fade in");
            view.clearAnimation();
            view.startAnimation(fade_in_animation);
            numCleared = numCleared - 1;
        }
        String clockName = getResources().getResourceEntryName(view.getId());
        int clockRow = Integer.parseInt(clockName.charAt(5) + "");
        int clockColumn = Integer.parseInt(clockName.charAt(6) + "");
        int getRow = 3;
        int getColumn = 2;
        View otherClock = ((View)view.getParent()).findViewById(getResources().getIdentifier("clock" + getRow + getColumn, "id", getPackageName()));



        if (numCleared == 8){
            Toast.makeText(this, "You win. Press button to play again.",Toast.LENGTH_LONG ).show();
            startActivity(new Intent(this, MyActivity3.class));
        }
    }
    public void onclockbuttonpush (final View view) {
        Log.d("kealinghornets.myapp", "lose");
        Toast.makeText(this, "WRONG! YOU LOSE!Press button to try again.", Toast.LENGTH_LONG).show();
        startActivity(new Intent(this, MyActivity.class));
    }





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_activity2);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_activity2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
